package com.wlstock.funi.ui;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ListView;

import com.em.baselistener.OnBackListener;
import com.em.baselistener.OnMoreListener;
import com.em.widget.view.SelectPopUpWindow.onPopViewListener;
import com.wlstock.funi.R;
import com.wlstock.funi.adapter.SelfFundSetAdapter;
import com.wlstock.funi.dao.SelfFundDao;
import com.wlstock.funi.dao.SelfFundSetDao;
import com.wlstock.funi.model.SelfFundSetModel;
import com.wlstock.funi.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 监测序列记录
 * @author admin
 *
 */
public class SelfFundSetHisActivity extends FundBaseFragmentActivity implements OnBackListener, onPopViewListener, OnMoreListener {
	
	private ListView lv_self_fund_set;
	private List<SelfFundSetModel> stkSetList;
	private SelfFundSetAdapter selfFundSetAdapter;
	private int filterType = SelfFundSetModel.PENDING;;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_selffund_set);
		setTitle(filterType);
		onBackClick(this);
		onMoreClick(this);
		findViewById(R.id.layout_top_edit).setVisibility(View.GONE);
		
		lv_self_fund_set = (ListView) findViewById(R.id.lv_self_fund_set);
		stkSetList = SelfFundSetDao.getInstance(this).queryByState(SelfFundSetModel.PENDING);
		selfFundSetAdapter = new SelfFundSetAdapter(this, stkSetList, R.layout.list_item_selffund_specific_set,-1);
		lv_self_fund_set.setAdapter(selfFundSetAdapter);

	}
	
	

	private List<Map<String, Object>> getMenuList() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("name", getResources().getString(R.string.pop_title_selffund_set_all_his));
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_valiable));
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_unavaliable));
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_enableall));
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put("name", getResources().getString(R.string.pop_title_delete_out_date_datas));
		list.add(map);

		return list;
	}



	@Override
	public void onBackClick(View v) {
		this.finish();
	} 

	@Override
	public void onViewSelectListener(String str) {
		setTitle(str);
		if(str.equals(getResources().getString(R.string.pop_title_selffund_set_all_his))){
			stkSetList.clear();
			stkSetList.addAll(SelfFundSetDao.getInstance(this).queryAll());
			selfFundSetAdapter.setmDatas(stkSetList);
			selfFundSetAdapter.notifyDataSetChanged();
			setTitle(filterType);
		} else if(str.equals(getResources().getString(R.string.pop_title_valiable))) {
			filterType  = SelfFundSetModel.PENDING;
			filterModles(filterType);
			setTitle(filterType);
		}else if (str.equals(getResources().getString(R.string.pop_title_unavaliable))) {
			filterType = SelfFundSetModel.DONE;
			filterModles(filterType);
			setTitle(filterType);
		}else if (str.equals(getResources().getString(R.string.pop_title_enableall))) {
			SelfFundSetDao.getInstance(this).updatePendState(SelfFundSetModel.DONE);
			filterModles(filterType);
		}else if (str.equals(getResources().getString(R.string.pop_title_delete_out_date_datas))) {
			List<SelfFundSetModel> invalidModels = SelfFundSetDao.getInstance(this).queryInvalidPendList(TimeUtils.getDateByDayOffset(new Date(),-3));
			SelfFundDao.getInstance(this).deleteWitnInvalidPendList(invalidModels);
		}
	}



	private void filterModles(int type) {
		stkSetList.clear();
		stkSetList.addAll(SelfFundSetDao.getInstance(this).queryByState(type));
		selfFundSetAdapter.notifyDataSetChanged();
	}
	
	@Override
	public void onMoreClick(View v) {
		showPopUpView(getMenuList(), this);
	}
	
	
	public void setTitle(int filterType) {
		switch (filterType) {
		case SelfFundSetModel.PENDING:
			setTitle(getResources().getString(R.string.pop_title_valiable));
			break;
		case SelfFundSetModel.DONE:
			setTitle(getResources().getString(R.string.pop_title_unavaliable));
			break;
		default:
			break;
		}

	}
}
